import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/widgets/tap_transition_widget.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';

enum SafeAreaType { both, top, bottom }

class Layout extends StatelessWidget {
  late bool hasHeader;
  late Widget? customHeader;
  late var leading;
  late Widget title;
  late double height;
  late Widget content;
  late Color color;
  late Widget? footer;
  late List<Widget> actions;
  late bool headerBorder;
  late bool inAsyncCall;
  late Widget? progressIndicator;
  late Widget? floatButton;
  late FloatingActionButtonLocation? floatButtonLocation;
  late SafeAreaType? safeAreaType;
  late bool footerShadow;

  Layout({
    Key? key,
    bool hasHeader = true,
    Widget? customHeader,
    leading = true,
    Object title = '',
    double height = 100,
    required Widget content,
    Color color = Colors.white,
    Widget? footer,
    List<Widget>? actions,
    bool headerBorder = false,
    bool inAsyncCall = false, //加载框 只需要调用接口前设置成true 返回接口设置成false即可
    Widget? floatButton,
    Widget? progressIndicator,
    FloatingActionButtonLocation? floatButtonLocation,
    SafeAreaType? safeAreaType = SafeAreaType.both,
    bool? footerShadow = false,
  }) {
    if (title is String) {
      this.title = Text(
        title,
        style: TextStyle(
          color: ColorConf.normal,
          fontSize: 18.sp,
        ),
      );
    } else if (title is Widget) {
      this.title = title;
    }
    if (leading is bool) {
      if (leading) {
        this.leading = Builder(
          builder: (ctx) => TapTransitionWidget(
            child: Container(
                color: Colors.transparent,
                padding: EdgeInsets.all(17.0.w),
                child: Image.asset(
                  'images/ic_back.png',
                  height: 16.w,
                  width: 16.w,
                  color: ColorConf.normal,
                )),
            onPressed: () {
              if (leading) {
                Get.back();
              }
            },
          ),
        );
      } else {
        this.leading = Container();
      }
    } else {
      this.leading = leading;
    }
    this.hasHeader = hasHeader;
    this.customHeader = customHeader;
    this.actions = actions ?? [];
    this.content = content;
    this.color = color;
    this.footer = footer;
    this.height = height;
    this.headerBorder = headerBorder;
    this.inAsyncCall = inAsyncCall;
    this.floatButton = floatButton;
    this.progressIndicator = progressIndicator;
    this.floatButtonLocation = floatButtonLocation;
    this.safeAreaType = safeAreaType;
    this.footerShadow = footerShadow!;
  }

  PreferredSizeWidget? _getHeader() {
    if (hasHeader) {
      return AppBar(
        brightness: Brightness.dark,
        titleSpacing: 0.0,
        title: SizedBox(
          width: double.infinity,
          height: height,
          child: customHeader == null
              ? Stack(
                  alignment: Alignment.center,
                  children: [
                    Positioned(
                      child: leading,
                      left: 0.0,
                    ),
                    title,
                    Positioned(
                      child: Row(
                        children: actions,
                      ),
                      right: 0.0,
                    )
                  ],
                )
              : customHeader,
        ),
        automaticallyImplyLeading: false,
        bottom: headerBorder
            ? PreferredSize(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        color: Color(0xFFEEEEEE),
                      ),
                    ),
                  ),
                ),
                preferredSize: Size.fromHeight(1.0.w),
              )
            : null,
        shadowColor: Color(0x00),
        backgroundColor: Colors.white,
      );
    }
  }

  @override
  Widget build(BuildContext ctx) {
    return ModalProgressHUD(
        child: Container(
          color: ColorConf.white,
          child: GestureDetector(
            child: SafeArea(
              top: [SafeAreaType.top, SafeAreaType.both].contains(safeAreaType),
              bottom: [SafeAreaType.bottom, SafeAreaType.both]
                  .contains(safeAreaType),
              child: Scaffold(
                backgroundColor: color,
                appBar: _getHeader(),
                body: GestureDetector(
                  onTap: () {
                    FocusManager.instance.primaryFocus?.unfocus();
                  },
                  child: content,
                ),
                floatingActionButton: floatButton,
                floatingActionButtonLocation: floatButtonLocation,
                bottomNavigationBar: footer != null
                    ? Container(
                        decoration: BoxDecoration(
                          boxShadow: footerShadow
                              ? [
                                  BoxShadow(
                                    color: Color(0x0F000000),
                                    offset: Offset(0.0, -1),
                                    blurRadius: 2.0,
                                  ),
                                ]
                              : null,
                        ),
                        child: footer,
                      )
                    : null,
              ),
            ),
            onTap: () {
              FocusScope.of(ctx).unfocus(); //全局设置
            },
          ),
        ),
        inAsyncCall: inAsyncCall,
        progressIndicator: progressIndicator ??
            Container(
              child: new Container(
                decoration: ShapeDecoration(
                  color: Colors.black54,
                  shape: RoundedRectangleBorder(),
                ),
                child: new Center(
                  //保证控件居中效果
                  child: new SizedBox(
                    width: 120.0.w,
                    height: 120.0.w,
                    child: new Container(
                      decoration: ShapeDecoration(
                        color: Color(0xffffffff),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(
                            Radius.circular(8.0.w),
                          ),
                        ),
                      ),
                      child: new Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          CupertinoActivityIndicator(),
                          new Padding(
                            padding: EdgeInsets.only(
                              top: 20.w,
                            ),
                            child: new Text(
                              "请稍等...",
                              style: new TextStyle(fontSize: 12.0.sp),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            ));
  }
}
